package utils

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
	"log"

	//"github.com/garyburd/redigo/redis"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql" // 引入mysql驱动
	"github.com/spf13/viper"
	//"time"
)

var DB *gorm.DB

func InitConfig() {
	viper.SetConfigName("app")
	viper.AddConfigPath("back\\config")
	err := viper.ReadInConfig()
	if err != nil {
		panic(err)
	}
	fmt.Println("config app:", viper.Get("mysql.dns"))

}

func InitMySQL() {
	var err error
	DB, err = gorm.Open("mysql", viper.GetString("mysql.dns"))
	if err != nil {
		panic("failed to connect database")
	}
	DB.LogMode(true)
	createTable()
}

//var RedisPool *redis.Pool
//
//func initPool(address string, maxIdle, maxActive int, IdleTimeout time.Duration) {
//	RedisPool = &redis.Pool{
//		MaxIdle:     maxIdle,
//		MaxActive:   maxActive,
//		IdleTimeout: IdleTimeout,
//		Dial: func() (redis.Conn, error) {
//			return redis.Dial("tcp", "127.0.0.1:6379")
//		},
//	}
//}
//
//func InitRedis() {
//	initPool("localhost:6379", 16, 9, 300*time.Second)
//}

var Ctx = context.Background()
var RedisClient *redis.ClusterClient

func InitRedis() {
	RedisClient = redis.NewClusterClient(&redis.ClusterOptions{
		Addrs: []string{"127.0.0.1:7001", "127.0.0.1:7002", "127.0.0.1:7003", "127.0.0.1:7004", "127.0.0.1:7005", "127.0.0.1:7006"},
	})
	if _, err := RedisClient.Ping(Ctx).Result(); err != nil {
		log.Fatalf("Redis cluster connection error: %v", err)
	}
}
